package tech.yunland.shop.domain.handler;

import com.moyuer.bsf.domain.model.Organ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tech.yunland.weixin.domain.model.OrganWexin;
import tech.yunland.weixin.domain.service.OrganWexinService;
import tech.yunland.weixin.handler.MessageHandler;
import tech.yunland.weixin.protocol.BaseArticle;
import tech.yunland.weixin.protocol.BaseMessage;
import tech.yunland.weixin.protocol.send.reply.NewsReplyMsg;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 李珂<likenamehaojie@hotmail.com>
 */
@Component("onSelfGoods")
public class OnSelfGoodsMessageHandler implements MessageHandler<BaseMessage> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${weixin.api.host_url}")
    private String host_url;
    @Autowired
    private OrganWexinService organWexinService;

    @Override
    public NewsReplyMsg deal(BaseMessage inputMessage) {

        NewsReplyMsg result = new NewsReplyMsg();
        result.setFromUser(inputMessage.getToUser());
        result.setToUser(inputMessage.getFromUser());
        result.setCreateTime(new Date());
        result.setType(BaseMessage.MsgType.news);

        List<BaseArticle> articleList = new ArrayList<BaseArticle>();
        BaseArticle article = new BaseArticle();
        article.setTitle("在售商品");
        article.setDescription("新品");
        //article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/Flmib3fPlr7rcwgoicyjjgmFf5GTVLc8xaBFFlLBmtgGraOjWLBaEYxJibswzm9HDNnIumk1MeflPQGC90eknU2wQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1");
        //article.setPicUrl(this.getPicUrl(organWexin));
        OrganWexin organWexin = organWexinService.getBywxNumber(inputMessage.getToUser());
        Organ organ = organWexin.getOwner();
        String organId = organ.getId();
        article.setUrl(host_url+"/#/goods-weixin?organId="+organId);

        articleList.add(article);
        result.setArticles(articleList);
        result.setArticleCount(articleList.size());

        return result;
    }


    private String getPicUrl(OrganWexin organWexin) {
        return organWexin.getShopPic()==null?"http://api.yunlandtech.com/uploads/52dd658e-7e54-4ac8-ada5-ee6524550a7f.jpg":organWexin.getShopPic();
    }

}
